package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lq.domain.bo.ReportLqBmjrBsInfoBo;
import com.ruoyi.report_lq.domain.vo.ReportLqBmjrBsInfoVo;
import com.ruoyi.report_lq.domain.ReportLqBmjrBsInfo;
import com.ruoyi.report_lq.mapper.ReportLqBmjrBsInfoMapper;
import com.ruoyi.report_lq.service.IReportLqBmjrBsInfoService;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

import static org.flowable.engine.impl.el.DateUtil.format;

/**
 * 沥青薄膜加热/旋转薄膜试验检测记录-新版本Service业务层处理
 *
 * @author zyb
 * @date 2024-10-30
 */
@RequiredArgsConstructor
@Service
@Transactional
public class ReportLqBmjrBsInfoServiceImpl implements IReportLqBmjrBsInfoService {

    private final ReportLqBmjrBsInfoMapper baseMapper;

    /**
     * 查询沥青薄膜加热/旋转薄膜试验检测记录-新版本
     */
    @Override
    @FIllBasicInfo
    public ReportLqBmjrBsInfoVo queryById(Long excelId){
        ReportLqBmjrBsInfoVo vo = baseMapper.selectVoById(excelId);
        setDecimalPlaces(vo);
        return vo;
    }

    private void setDecimalPlaces(ReportLqBmjrBsInfoVo vo) {
        DecimalFormat df = new DecimalFormat("0.0");
        vo.setD1(Objects.nonNull(vo.getD1()) ? new BigDecimal(df.format(vo.getD1())) : null);
        vo.setD2(Objects.nonNull(vo.getD2()) ? new BigDecimal(df.format(vo.getD2())) : null);
        vo.setE1(Objects.nonNull(vo.getE1()) ? new BigDecimal(df.format(vo.getE1())) : null);
        vo.setG3(Objects.nonNull(vo.getG3()) ? new BigDecimal(df.format(vo.getG3())) : null);
        vo.setG4(Objects.nonNull(vo.getG4()) ? new BigDecimal(df.format(vo.getG4())) : null);
    }

    /**
     * 查询沥青薄膜加热/旋转薄膜试验检测记录-新版本列表
     */
    @Override
    public TableDataInfo<ReportLqBmjrBsInfoVo> queryPageList(ReportLqBmjrBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqBmjrBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportLqBmjrBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青薄膜加热/旋转薄膜试验检测记录-新版本列表
     */
    @Override
    public List<ReportLqBmjrBsInfoVo> queryList(ReportLqBmjrBsInfoBo bo) {
        LambdaQueryWrapper<ReportLqBmjrBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqBmjrBsInfo> buildQueryWrapper(ReportLqBmjrBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqBmjrBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportLqBmjrBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportLqBmjrBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportLqBmjrBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportLqBmjrBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportLqBmjrBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportLqBmjrBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportLqBmjrBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportLqBmjrBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportLqBmjrBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportLqBmjrBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportLqBmjrBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportLqBmjrBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportLqBmjrBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportLqBmjrBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportLqBmjrBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportLqBmjrBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportLqBmjrBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportLqBmjrBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportLqBmjrBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportLqBmjrBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportLqBmjrBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(bo.getA1() != null, ReportLqBmjrBsInfo::getA1, bo.getA1());
        lqw.eq(bo.getB1() != null, ReportLqBmjrBsInfo::getB1, bo.getB1());
        lqw.eq(bo.getC1() != null, ReportLqBmjrBsInfo::getC1, bo.getC1());
        lqw.eq(bo.getD1() != null, ReportLqBmjrBsInfo::getD1, bo.getD1());
        lqw.eq(bo.getE1() != null, ReportLqBmjrBsInfo::getE1, bo.getE1());
        lqw.eq(bo.getA2() != null, ReportLqBmjrBsInfo::getA2, bo.getA2());
        lqw.eq(bo.getB2() != null, ReportLqBmjrBsInfo::getB2, bo.getB2());
        lqw.eq(bo.getC2() != null, ReportLqBmjrBsInfo::getC2, bo.getC2());
        lqw.eq(bo.getD2() != null, ReportLqBmjrBsInfo::getD2, bo.getD2());
        lqw.eq(bo.getA3() != null, ReportLqBmjrBsInfo::getA3, bo.getA3());
        lqw.eq(bo.getB3() != null, ReportLqBmjrBsInfo::getB3, bo.getB3());
        lqw.eq(bo.getC3() != null, ReportLqBmjrBsInfo::getC3, bo.getC3());
        lqw.eq(bo.getD3() != null, ReportLqBmjrBsInfo::getD3, bo.getD3());
        lqw.eq(bo.getE3() != null, ReportLqBmjrBsInfo::getE3, bo.getE3());
        lqw.eq(bo.getF3() != null, ReportLqBmjrBsInfo::getF3, bo.getF3());
        lqw.eq(bo.getG3() != null, ReportLqBmjrBsInfo::getG3, bo.getG3());
        lqw.eq(bo.getH3() != null, ReportLqBmjrBsInfo::getH3, bo.getH3());
        lqw.eq(bo.getD4() != null, ReportLqBmjrBsInfo::getD4, bo.getD4());
        lqw.eq(bo.getE4() != null, ReportLqBmjrBsInfo::getE4, bo.getE4());
        lqw.eq(bo.getF4() != null, ReportLqBmjrBsInfo::getF4, bo.getF4());
        lqw.eq(bo.getG4() != null, ReportLqBmjrBsInfo::getG4, bo.getG4());
        lqw.eq(bo.getA5() != null, ReportLqBmjrBsInfo::getA5, bo.getA5());
        lqw.eq(bo.getB5() != null, ReportLqBmjrBsInfo::getB5, bo.getB5());
        lqw.eq(bo.getC5() != null, ReportLqBmjrBsInfo::getC5, bo.getC5());
        lqw.eq(bo.getD5() != null, ReportLqBmjrBsInfo::getD5, bo.getD5());
        lqw.eq(bo.getE5() != null, ReportLqBmjrBsInfo::getE5, bo.getE5());
        lqw.eq(bo.getF5() != null, ReportLqBmjrBsInfo::getF5, bo.getF5());
        lqw.eq(bo.getG5() != null, ReportLqBmjrBsInfo::getG5, bo.getG5());
        lqw.eq(bo.getA6() != null, ReportLqBmjrBsInfo::getA6, bo.getA6());
        lqw.eq(bo.getB6() != null, ReportLqBmjrBsInfo::getB6, bo.getB6());
        lqw.eq(bo.getC6() != null, ReportLqBmjrBsInfo::getC6, bo.getC6());
        lqw.eq(bo.getD6() != null, ReportLqBmjrBsInfo::getD6, bo.getD6());
        lqw.eq(bo.getE6() != null, ReportLqBmjrBsInfo::getE6, bo.getE6());
        lqw.eq(bo.getF6() != null, ReportLqBmjrBsInfo::getF6, bo.getF6());
        lqw.eq(bo.getG6() != null, ReportLqBmjrBsInfo::getG6, bo.getG6());
        lqw.eq(bo.getA7() != null, ReportLqBmjrBsInfo::getA7, bo.getA7());
        lqw.eq(bo.getB7() != null, ReportLqBmjrBsInfo::getB7, bo.getB7());
        lqw.eq(bo.getC7() != null, ReportLqBmjrBsInfo::getC7, bo.getC7());
        lqw.eq(bo.getD7() != null, ReportLqBmjrBsInfo::getD7, bo.getD7());
        lqw.eq(bo.getE7() != null, ReportLqBmjrBsInfo::getE7, bo.getE7());
        lqw.eq(bo.getF7() != null, ReportLqBmjrBsInfo::getF7, bo.getF7());
        lqw.eq(bo.getD8() != null, ReportLqBmjrBsInfo::getD8, bo.getD8());
        lqw.eq(bo.getE8() != null, ReportLqBmjrBsInfo::getE8, bo.getE8());
        lqw.eq(StringUtils.isNotBlank(bo.getA9()), ReportLqBmjrBsInfo::getA9, bo.getA9());
        lqw.eq(bo.getB9() != null, ReportLqBmjrBsInfo::getB9, bo.getB9());
        lqw.eq(bo.getC9() != null, ReportLqBmjrBsInfo::getC9, bo.getC9());
        lqw.eq(bo.getD9() != null, ReportLqBmjrBsInfo::getD9, bo.getD9());
        lqw.eq(bo.getE9() != null, ReportLqBmjrBsInfo::getE9, bo.getE9());
        lqw.eq(bo.getF9() != null, ReportLqBmjrBsInfo::getF9, bo.getF9());
        lqw.eq(bo.getG9() != null, ReportLqBmjrBsInfo::getG9, bo.getG9());
        lqw.eq(StringUtils.isNotBlank(bo.getA10()), ReportLqBmjrBsInfo::getA10, bo.getA10());
        lqw.eq(bo.getB10() != null, ReportLqBmjrBsInfo::getB10, bo.getB10());
        lqw.eq(bo.getC10() != null, ReportLqBmjrBsInfo::getC10, bo.getC10());
        lqw.eq(bo.getD10() != null, ReportLqBmjrBsInfo::getD10, bo.getD10());
        lqw.eq(bo.getF10() != null, ReportLqBmjrBsInfo::getF10, bo.getF10());
        return lqw;
    }

    /**
     * 新增沥青薄膜加热/旋转薄膜试验检测记录-新版本
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(ReportLqBmjrBsInfoBo bo) {
        ReportLqBmjrBsInfo add = BeanUtil.toBean(bo, ReportLqBmjrBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改沥青薄膜加热/旋转薄膜试验检测记录-新版本
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(ReportLqBmjrBsInfoBo bo) {
        ReportLqBmjrBsInfo update = BeanUtil.toBean(bo, ReportLqBmjrBsInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqBmjrBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青薄膜加热/旋转薄膜试验检测记录-新版本
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
